package org.javabasics.collection;

import java.util.Comparator;
import java.util.TreeSet;

/**
 * Created by LUOLIANG on 2016/8/29.
 * TreeSet会根据元素的实际大小进行排序
 */
public class MyTreeSet {
    public static void main(String[] args) {
        TreeSet treeSet = new TreeSet();

        treeSet.add(new C(90));
        treeSet.add(new C(-12));
        treeSet.add(new C(23));
        treeSet.add(new C(1));
        treeSet.add(new C(-3));

        System.out.println("TreeSet打印为：" + treeSet);
        System.out.println("第一个元素为：" + treeSet.first());
        System.out.println("最后一个元素为: " + treeSet.last());
    }
}

class C implements Comparable {
    private int age;

    public int getAge() {
        return age;
    }

    public C(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "C{" +
                "age=" + age +
                '}';
    }

    @Override
    public int compareTo(Object o) {
        C c = (C) o;

        return this.age > c.age ? -1 : this.age < c.age ? 1 : 0;
    }
}